<?php
namespace LEA\UserBundle\Controller;

use LEA\UserBundle\Utility\Utility;
use LEA\UserBundle\Utility\Auth;
use LEA\UserBundle\SQL\dbmngt\queriesEtud;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller{
    public function indexAction(){
        return $this->render('LEAUserBundle:Default:index.html.php');
    }

    public function connexionAction($user){
    	$request=$this->getRequest();
    	$session = $request->getSession();
    	$auth = new Auth();
    	$utils = new Utility();
    	$conn = $this->container->get('database_connection');

    	if (!$session->has('CK_USER') || strlen($session->get('CK_USER')==0) || $session->get('CK_USER') != $user) {
	    	$session->set('CK_USER', $user);
	    	$auth->checkUserRole($request, $conn);
	    }

          $doRolesUpdate=false;
          $refYear= 2011;   #$utils->getParam('REF_YEAR',date("Y"), $request);
          if (!$session->has('REF_YEAR')|| ($session->get('REF_YEAR')!=$refYear))
          	$doRolesUpdate=true;

          $session->set('REF_YEAR', $refYear);

           $refFormationRef=$utils->getParam('REF_FORMATIONTYPE',"FA/FI", $request);
          if ($session->has('REF_FORMATIONTYPE')|| ($session->get('REF_FORMATIONTYPE')!=$refFormationRef))
          	$doRolesUpdate=true;

          $session->set('REF_FORMATIONTYPE', $refFormationRef);


         if ($doRolesUpdate==true)
	          $auth->checkUserRole($request, $conn);

		 //echo($session->get('CK_ROLES')) ;

         if($auth->hasRole($request, 'PROF_ROLE') || $auth->hasRole($request, 'SECR_ROLE') || $auth->hasRole($request, 'RESP')){
	          $array = $this->responsableArray($user, $refYear, $request, $auth) ;
	          return $this->render('LEAUserBundle:Default:responsable.html.php', $array) ;
         }
         else{
         	if($auth->hasRole($request, 'STUD_ROLE')){

	           $array = $this->etudiantArray($conn, $user, $refYear, $session) ;
	           return $this->render('LEAUserBundle:Default:etudiant.html.php', $array) ;
	        }
	    }
        return $this->indexAction();
    }

    private function etudiantArray($conn, $student, $refYear, $session){
        $queriesEtud = new QueriesEtud($conn);
		$formation=$queriesEtud->doGetFormationForStudent($student,$refYear, $session);

		$tuteur = $queriesEtud->doGetTuteurInfoForStudent($student,$refYear, $session);
		$altRef=$queriesEtud->doGetAltRefForStudent($student,$refYear,$session);

		$refFormationRef=substr($formation,strlen($formation)-2,2);
		$session->set('REF_FORMATIONTYPE',$refFormationRef);

		$formationType=($refFormationRef=="FA")?"alternance":"stage";

		return array('tuteur' => $tuteur,
		'formation' => $formation,
		'formationType' => $formationType,
		'altRef' => $altRef,
		'user' => $student,
		'refYear' => $refYear);
    }

    private function responsableArray( $user, $refYear, $request, $auth){

    	if($auth->hasRole($request, 'PROF_ROLE')){
    		$arrayProf = array("Mes étudiants","Mes choix","Choisir étudiants","Rapport &amp; Soutenance");
    		$menuProf=array("lea_dumb_route","lea_dumb_route","lea_prof_choix_etud","lea_dumb_route","lea_dumb_route");
		}
    	$arraySecr =null;
    	if($auth->hasRole($request, 'SECR_ROLE')){
			$arraySecr = array("Etudiants par formation", "Etudiants par entreprise", "Etudiants par tuteur","Referents par entreprise"," Gestion des Bureaux") ;
			$menuSecr=array("lea_dumb_route","lea_dumb_route","lea_dumb_route","lea_dumb_route","lea_dumb_route");
		}
		$arrayResp = null;
		if($auth->hasRole($request, 'RESP')){
			$arrayResp = array("Encadrement par étud.","par tuteur","Actions","Attribution","Rapport &amp; Soutenance","Stats","Inscrire Etud","Admin");
			$menuResp=array("lea_dumb_route","lea_dumb_route","lea_resp_action","lea_dumb_route","lea_dumb_route","lea_dumb_route","lea_dumb_route","lea_dumb_route");
		}

		return array('user' => $user,
					 'refYear' => $refYear,
					 'arrayProf' => $arrayProf,
					 'arraySecr' => $arraySecr,
					 'arrayResp' => $arrayResp,
					 'menuProf' => $menuProf,
					 'menuSecr' => $menuSecr,
					 'menuResp' => $menuResp
                );
    }
}